/*oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO*/
/*
This files contains functions declarations related to user interface of HW assignment 1. 

Created by Amir Balva for the course "Introduction To Systems Programming", 
School of Electrical Engineering, Tel-Aviv University. winter 2014.
*/
/*oOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoOoO*/

#define _CRT_SECURE_NO_WARNINGS
#include "HW1_UI.h"
#include <string.h>

#define NUM_OF_REQUESTS 19

static Request *requests_array [NUM_OF_REQUESTS];
static int current_request;
static Email * email_buffer;
static int num_of_emails_in_buffer;

static Email * email_to_grandma;

//This function allocated memory for new Request struct and returns it's address
Request * Create_New_Request ()
{
	return (Request*)malloc(sizeof(Request));
}

//This function copys an Email from source to destination. Source must be provided, destination must be allocated.
void Copy_Email (Email * dest, Email * source)
{
	dest->date=source->date;
	dest->hour=source->hour;
	dest->ID=source->ID;
	dest->isMarked=source->isMarked;
	dest->isRead=source->isRead;
	strcpy(dest->content,source->content);
	strcpy(dest->subject,source->subject);
	strcpy(dest->sender,source->sender);
	strcpy(dest->recipient,source->recipient);
}


void Load_Emails_To_UI_Memory(Email* email_array, int num_of_emails)
{
	int i;
	if (num_of_emails<=0)
		return;
	email_buffer=(Email*)malloc(num_of_emails*sizeof(Email));
	for (i=0;i<num_of_emails;i++)
	{
		Copy_Email(&(email_buffer[i]),&(email_array[i]));
	}
	num_of_emails_in_buffer=num_of_emails;
}


void Print_One_Email(Email * email)
{
	printf("Email ID: %d . ",email->ID);
	printf("This e-mail %s Read. This e-mail %s marked as important\n", email->isRead==T ? "was":"wasn't", email->isMarked==T ? "was":"wasn't");
	printf("Date: %d , Hour: %d \n", email->date, email->hour);
	printf("From: %s , To: %s \n" , email->sender , email->recipient);
	printf("Subject: %s \n",email->subject);
	printf("Content:\n%s \n\n\n", email->content);
}

void Print_One_Email_Brief(Email * email)
{
	printf("Email ID: %d . ",email->ID);
	printf("This e-mail %s Read. This e-mail %s marked as important\n", email->isRead==T ? "was":"wasn't", email->isMarked==T ? "was":"wasn't");
	printf("Subject: %s \n\n\n",email->subject);
}

void Print_Emails()
{
	int i;
	for (i=0; i<num_of_emails_in_buffer;i++)
	{
		Print_One_Email(&(email_buffer[i]));
	}
	num_of_emails_in_buffer=0;
	free(email_buffer);
}

void Print_Emails_Brief()
{
	int i;
	for (i=0; i<num_of_emails_in_buffer;i++)
	{
		Print_One_Email_Brief(&(email_buffer[i]));
	}
	num_of_emails_in_buffer=0;
	free(email_buffer);
}



void Initilize_UI(const char * SetupString)
{
	//Ram 8_11_14 test entrance
	//printf("in module: %s function: %s, input is: %s\r\n", "HW1_UI.c", "Initilize_UI", SetupString);
	//
	Request * req;
	Email* email;
	current_request=0;

	email_to_grandma=(Email*)malloc(sizeof(Email));
	email=email_to_grandma;
	email->isRead=F;
	email->isMarked=F;
	email->ID=666;
	email->hour=132200;
	email->date=20141104;
	strcpy(email->recipient,"grandma@family.com");
	strcpy(email->sender,"loving_grandson@family.com");
	strcpy(email->subject,"Hi Grandma");
	strcpy(email->content,"Hi Grandma, wish you happy birthday. Love you,\n Your grandson. <3");




	requests_array[0]=Create_New_Request();
	req=requests_array[0];
	req->operation=DOWNLOAD_EMAILS;

	requests_array[1]=Create_New_Request();
	req=requests_array[1];
	req->operation=GET_EMAILS_TO_UI;

	requests_array[2]=Create_New_Request();
	req=requests_array[2];
	req->operation=PRINT_EMAILS_BRIEF;

	requests_array[3]=Create_New_Request();
	req=requests_array[3];
	req->operation=MARK_AS_READ;
	req->ID=2;

	requests_array[4]=Create_New_Request();
	req=requests_array[4];
	req->operation=MARK_IMPORTANT_EMAIL;
	req->ID=21;

	requests_array[5]=Create_New_Request();
	req=requests_array[5];
	req->operation=DELETE_EMAIL;
	req->ID=1;

	requests_array[6]=Create_New_Request();
	req=requests_array[6];
	req->operation=DOWNLOAD_EMAILS;

	requests_array[7]=Create_New_Request();
	req=requests_array[7];
	req->operation=GET_EMAILS_TO_UI;

	requests_array[8]=Create_New_Request();
	req=requests_array[8];
	req->operation=PRINT_EMAILS_BRIEF;

	requests_array[9]=Create_New_Request();
	req=requests_array[9];
	req->operation=UNMARK_IMPORTANT_EMAIL;
	req->ID=21;

	requests_array[10]=Create_New_Request();
	req=requests_array[10];
	req->operation=CREATE_EMAIL;
	req->email=email_to_grandma;

	requests_array[11]=Create_New_Request();
	req=requests_array[11];
	req->operation=DOWNLOAD_EMAILS;

	requests_array[12]=Create_New_Request();
	req=requests_array[12];
	req->operation=MARK_AS_READ;
	req->ID=10;

	requests_array[13]=Create_New_Request();
	req=requests_array[13];
	req->operation=GET_EMAILS_TO_UI;

	requests_array[14]=Create_New_Request();
	req=requests_array[14];
	req->operation=PRINT_EMAILS_BRIEF;

	requests_array[15]=Create_New_Request();
	req=requests_array[15];
	req->operation=DOWNLOAD_EMAILS;

	requests_array[16]=Create_New_Request();
	req=requests_array[16];
	req->operation=GET_EMAILS_TO_UI;

	requests_array[17]=Create_New_Request();
	req=requests_array[17];
	req->operation=PRINT_EMAILS_FULL;

	requests_array[18]=Create_New_Request();
	req=requests_array[18];
	req->operation=CLOSE_PROGRAM;

	return;
}


Request * Get_New_User_Request()
{
	if (current_request<NUM_OF_REQUESTS)
		return requests_array[current_request++];
	else
	{
		printf("Error! Quit program was already assigned!");
		return NULL;
	}
}



void Close_UI()
{
	//Ram 8_11_14 test entrance
	//printf("in module: %s function: %s\r\n", "HW1_UI.c", "Close_UI");
	//
	int i;
	for (i=0;i<NUM_OF_REQUESTS;i++)
		free (requests_array[i]);
	free (email_to_grandma);
	printf("UI Closed Successfully!\n");
}